//
//  RYCHTTPViewController.m
//  RaiyiCommon
//
//  Created by zhiwei ma on 12-4-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RYCHTTPViewController.h"
#import <objc/runtime.h>

#import "RYCModalView.h"
#import "UIView+Additional.h"

@implementation UIViewController (RYCHTTP)
#ifdef _EW_HTTP_REQUEST_MODEL_
static char* sHttpModel = "httpModel";
#endif
static char* sHttpImageQueue = "httpImageQueue";

#ifdef _EW_HTTP_REQUEST_MODEL_
- (RYCHTTPRequestModel*)httpModel
{
    RYCHTTPRequestModel* model = objc_getAssociatedObject(self, sHttpModel);
    if (nil == model)
    {
        self.httpModel = [self httpRequestModel];
    }
    return objc_getAssociatedObject(self, sHttpModel);
}

- (void)setHttpModel:(RYCHTTPRequestModel *)httpModel
{
    objc_setAssociatedObject(self, sHttpModel, httpModel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#endif

- (RYCHTTPImageQueue*)httpImageQueue
{
    RYCHTTPImageQueue* queue = objc_getAssociatedObject(self, sHttpImageQueue);
    if (nil == queue)
    {
        self.httpImageQueue = [[[RYCHTTPImageQueue alloc] init] autorelease];
    }
    return objc_getAssociatedObject(self, sHttpImageQueue);
}

- (void)setHttpImageQueue:(RYCHTTPImageQueue *)httpImageQueue
{
    objc_setAssociatedObject(self, sHttpImageQueue, httpImageQueue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#ifdef _EW_HTTP_REQUEST_MODEL_
- (RYCHTTPRequestModel*)httpRequestModel
{
    return nil;
}
#endif
- (void)makeComponentViewsEnabled:(BOOL)aEnabled
{
}

- (void)startWaiting
{
    [self.view startWaiting];
    [self makeComponentViewsEnabled:NO];
}

- (void)stopWaiting
{
    [self.view stopWaiting];
    [self makeComponentViewsEnabled:YES];
}
@end

@interface RYCHTTPViewController ()
- (BOOL)httpModelRequestDataIfNeed;
@end

@implementation RYCHTTPViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (self.httpModelRequestDataPolicy & RYCHTTPModelRequestDataPolicyViewDidLoad)
    {
        [self httpModelRequestDataIfNeed];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.httpModelRequestDataPolicy & RYCHTTPModelRequestDataPolicyViewWillAppear)
    {
        [self httpModelRequestDataIfNeed];
    }

    [self.httpImageQueue setSuspended:NO];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (self.httpModelRequestDataPolicy & RYCHTTPModelRequestDataPolicyViewDidAppear)
    {
        [self httpModelRequestDataIfNeed];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
#ifdef _EW_HTTP_REQUEST_MODEL_
    [self.httpModel cancelRequest];
#endif
    [self.httpImageQueue setSuspended:YES];
}

#pragma mark private mothods
- (BOOL)httpModelRequestDataIfNeed
{
#ifdef _EW_HTTP_REQUEST_MODEL_
    if (self.httpModel)
    {
        self.httpModel.delegate = self;
        if ([self.httpModel isFinished])
        {
            return NO;
        }
        else
        {
            [self.httpModel sendRequest];
            [self startWaiting];
            [self makeComponentViewsEnabled:NO];
            return YES;
        }
    }
#endif
    return NO;
}

#pragma mark RYCHTTPRequestModelDelegate
- (void)didHTTPRequestModelFinished:(RYCHTTPRequestModel*)aModel
{
    [self.view stopWaiting];
    [self makeComponentViewsEnabled:YES];
}

- (void)didHTTPRequestModelFailed:(RYCHTTPRequestModel*)aModel
{
    [self.view stopWaiting];
    [self makeComponentViewsEnabled:YES];
    [RYCModalAlertView messageBox:nil message:aModel.error.localizedDescription];
}
@end

